﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.HomeServer.Controls;

namespace RouterControl.Whs.Wizards
{
    public enum WizardPageClientButtonText
    {
        NoButton,
        Apply,
        Back,
        Cancel,
        Close,
        Done,
        Finish,
        Next,
        Ok,
        Refresh,
        RestartLater,
        RestartNow,
        Retry
    }

    public partial class WizardPageClientBase : UserControl
    {
        private WizardPage _wizardPage;

        public Wizard ParentWizard
        {
            get;
            internal set;
        }

        private bool _applyButtonEnabled = true;

        public bool ApplyButtonEnabled
        {
            get
            { 
                return _applyButtonEnabled; 
            }
            set
            {
                _applyButtonEnabled = value;

                if (_wizardPage != null)
                    _wizardPage.ApplyButtonEnabled = value;
            }
        }

        private bool _backButtonEnabled = true;

        public bool BackButtonEnabled
        {
            get
            {
                return _backButtonEnabled;
            }
            set
            {
                _backButtonEnabled = value;

                if (_wizardPage != null)
                    _wizardPage.BackButtonEnabled = value;
            }
        }

        private bool _cancelButtonEnabled = true;

        public bool CancelButtonEnabled
        {
            get
            {
                return _cancelButtonEnabled;
            }
            set
            {
                _cancelButtonEnabled = value;

                if (_wizardPage != null)
                    _wizardPage.CancelButtonEnabled = value;
            }
        }

        private bool _okButtonEnabled = true;

        public bool OkButtonEnabled
        {
            get
            {
                return _okButtonEnabled;
            }
            set
            {
                _okButtonEnabled = value;

                if (_wizardPage != null)
                    _wizardPage.OkButtonEnabled = value;
            }
        }

        private WizardPageClientButtonText _applyButtonName = WizardPageClientButtonText.NoButton;

        public WizardPageClientButtonText ApplyButtonName
        {
            get
            {
                return _applyButtonName;
            }
            set
            {
                _applyButtonName = value;

                if (_wizardPage != null)
                    _wizardPage.ApplyButtonName = value.ToWhsString();
            }
        }

        private WizardPageClientButtonText _backButtonName = WizardPageClientButtonText.Back;

        public WizardPageClientButtonText BackButtonName
        {
            get
            {
                return _backButtonName;
            }
            set
            {
                _backButtonName = value;

                if (_wizardPage != null)
                    _wizardPage.BackButtonName = value.ToWhsString();
            }
        }

        private WizardPageClientButtonText _cancelButtonName = WizardPageClientButtonText.Cancel;

        public WizardPageClientButtonText CancelButtonName
        {
            get
            {
                return _cancelButtonName;
            }
            set
            {
                _cancelButtonName = value;

                if (_wizardPage != null)
                    _wizardPage.CancelButtonName = value.ToWhsString();
            }
        }

        private WizardPageClientButtonText _okButtonName = WizardPageClientButtonText.Next;

        public WizardPageClientButtonText OkButtonName
        {
            get
            {
                return _okButtonName;
            }
            set
            {
                _okButtonName = value;

                if (_wizardPage != null)
                    _wizardPage.OkButtonName = value.ToWhsString();
            }
        }

        private List<EventHandler> _applyButtonClickEvents = new List<EventHandler>();

        public event EventHandler ApplyButtonClick
        {
            add
            {
                _applyButtonClickEvents.Add(value);

                if (_wizardPage != null)
                    _wizardPage.ApplyButtonhook(value);
            }
            remove
            {
                throw new NotImplementedException("not supported by WizardPage");
            }
        }

        private List<EventHandler> _backButtonClickEvents = new List<EventHandler>();

        public event EventHandler BackButtonClick
        {
            add
            {
                _backButtonClickEvents.Add(value);

                if (_wizardPage != null)
                    _wizardPage.BackButtonhook(value);
            }
            remove
            {
                throw new NotImplementedException("not supported by WizardPage");
            }
        }

        private List<EventHandler> _cancelButtonClickEvents = new List<EventHandler>();

        public event EventHandler CancelButtonClick
        {
            add
            {
                _cancelButtonClickEvents.Add(value);

                if (_wizardPage != null)
                    _wizardPage.CancelButtonhook(value);
            }
            remove
            {
                throw new NotImplementedException("not supported by WizardPage");
            }
        }

        private List<EventHandler> _okButtonClickEvents = new List<EventHandler>();

        public event EventHandler OkButtonClick
        {
            add
            {
                _okButtonClickEvents.Add(value);

                if (_wizardPage != null)
                    _wizardPage.OkButtonhook(value);
            }
            remove
            {
                throw new NotImplementedException("not supported by WizardPage");
            }
        }

        /// <summary>
        /// use this function to register events and enable/disable buttons
        /// </summary>
        internal void SetWizardPage(WizardPage wp, bool preInit)
        {
            if (wp == null)
            {
                _wizardPage = null;

                return;
            }

            if (preInit)
            {
                // button names
                wp.ApplyButtonName = _applyButtonName.ToWhsString();
                wp.BackButtonName = _backButtonName.ToWhsString();
                wp.CancelButtonName = _cancelButtonName.ToWhsString();
                wp.OkButtonName = _okButtonName.ToWhsString();
            }
            else
            {
                // button states and event handler
                if (wp.ApplyButtonName != null)
                {
                    wp.ApplyButtonEnabled = _applyButtonEnabled;

                    foreach (var e in _applyButtonClickEvents)
                        wp.ApplyButtonhook(e);
                }

                if (wp.BackButtonName != null)
                {
                    wp.BackButtonEnabled = _backButtonEnabled;

                    foreach (var e in _backButtonClickEvents)
                        wp.BackButtonhook(e);
                }

                if (wp.CancelButtonName != null)
                {
                    wp.CancelButtonEnabled = _cancelButtonEnabled;

                    foreach (var e in _cancelButtonClickEvents)
                        wp.CancelButtonhook(e);
                }

                if (wp.OkButtonName != null)
                {
                    wp.OkButtonEnabled = _okButtonEnabled;

                    foreach (var e in _okButtonClickEvents)
                        wp.OkButtonhook(e);
                }

                // finalize, grant public access
                _wizardPage = wp;
            }
        }

        public string TitleText
        {
            get;
            set;
        }

        public void SetCurrentPage<T_Page>() where T_Page : WizardPageClientBase, new()
        {
            if (this.ParentWizard == null)
                throw new InvalidOperationException("no parent wizard set, yet");

            try
            {
	            this.ParentWizard.SetCurrentPanel(typeof(WizardPageHost<T_Page>));
            }
            catch (KeyNotFoundException ex)
            {
                throw new ArgumentException("specified panel was not registered; see your wizard implementation: Wizard.SetTypes()", "T_Page", ex);
            }
        }

        /// <summary>
        /// put code here to handle inside the wizard page
        /// </summary>
        public virtual void StartRealWork()
        {

        }

        public WizardPageClientBase()
        {
            InitializeComponent();
        }
    }
}
